#ifndef _RETRO_RESOURCEBROWSER
#define _RETRO_RESOURCEBROWSER

namespace Retro
{
  namespace Creator
  {
    namespace Toolbox
    {
      enum ResourceDisplay
      {
        ResourceDisplayActive,
        ResourceDisplayUnsaved,
        ResourceDisplayDeleted,
        ResourceDisplayCorrupted,
        ResourceDisplayAll,
      };

      enum BrowserNodeType
      {
        BrowserNodeResourceType,
        BrowserNodeResource,
        BrowserNodeFolder,
      };

      struct BrowserNode
      {
        union
        {
          Data::ResourceType  resourceType;
          Data::Resource*     resource;
          HierarchyPath*      path;
        };

        HTREEITEM       treeItem;
        BrowserNodeType type;
      };

      class Browser : public Windows::Window
      {
      private:
        Generic::Vector<BrowserNode*, 10> mNodes;

        WrapWnd         mTree;
        LayoutElement   mLayout;

        Menu   mMenuResource;
        Menu   mMenuResourceType;
        Menu   mMenuFolder;
        Menu   mMenuDisplay;

        INT    mIconFile;
        INT    mIconFolder;

        ResourceDisplay     mDisplay;
        Data::RetroProject* mProject;
        ToolBar             mToolbar;
        BrowserNode*        mDragNode;

        TCHAR               listItemText[1000];
        TVINSERTSTRUCT      listItem;
        HTREEITEM           listRoots[
          Data::ResourceLast - Data::ResourceFirst + 1
        ];

        VOID                getResourceText(Data::Resource* value);
        BrowserNode*        findResource(Data::Resource* iValue);
        BrowserNode*        addFolder(HierarchyPath* iValue);
        BrowserNode*        findFolder(INT iID);
        BrowserNode*        findRoot(BrowserNode* iNode);
        VOID                initItem(BrowserNode* iNode);
        VOID                removeNode(BrowserNode* iNode);
        BrowserNode*        addNewFolder(const BrowserNode* iParent);
        const BrowserNode*  addResource(Data::Resource* value);
        VOID                openResource(BrowserNode* iNode);
        VOID                openFolder(BrowserNode* iFolder, BOOL iDescendants);

        VOID                clearFolder(
          BrowserNode* iFolder,
          BrowserNode* iNewParent
          );
        VOID                deleteFolder(
          BrowserNode* iFolder
          );

        VOID                insertResource(
          HTREEITEM iParent,
          Data::Resource* iValue
          );

        VOID                moveNode(
          BrowserNode* iNode,
          const BrowserNode* iParent
          );

        VOID                beginDrag(BrowserNode* iNode);

      protected:
        virtual VOID    onCreate(Event::WndCreateEvent& iEvent);
        virtual VOID    onDestroy();
        virtual VOID    onSize(Event::SizeEvent& iEvent);
        virtual VOID    onNotify(Event::NotifyEvent& iEvent);
        virtual VOID    onCommand(Event::CommandEvent& iEvent);
        virtual VOID    onFocus(Event::FocusEvent& iEvent);
        virtual VOID    onMouseMove(Event::MouseEvent& iEvent);
        virtual VOID    onMouseUp(Event::MouseEvent& iEvent);

      public:
        INL ResourceDisplay getDisplay()
        {
          return mDisplay;
        }

        VOID                setDisplay(ResourceDisplay value);
        VOID                setProject(Data::RetroProject* project);

        VOID                selectResource(Data::Resource* value);
        BrowserNode*        getSelectedNode();
        BrowserNode*        getHoveredNode();

        VOID                updateList();
        VOID                sortList();

        Browser();
        ~Browser();
      };
    } // namespace Toolbox
  } // namespace Creator
} // namespace Retro

#endif